package org.example.threadlocaltest.demos.configure;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service//该接口是security过滤链中 usernamePasswordAuthenticationFilter 过滤器中调用的，用于根据用户名查询用户信息
// 这里一定要加上这个注解， 一旦加上了，就不会走从内存中查询用户信息的逻辑了，就走这个类里面的逻辑了
public class MyUserDetailService implements UserDetailsService {

    //实际上这里应该是 自动注入我们的 用户Service


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //实际上这里就应该 根据 username 去数据库中查询用户信息，用上面注入的 用户Service 去查询信息
//        然后将其封装成 MyUserDetails 返回

        //我这里就模拟一下
        if ("admin".equals(username)) {
            return new MyUserDetails("admin",
                    "$2a$10$WQMloBfgYWA6eFQ/98WBB.Cj9T0NMDwi1FG9U1A0IQocpBBehlYqa",
                    List.of(new SimpleGrantedAuthority("ROLE_ADMIN")));
        } else {
            return new MyUserDetails("user",
                    "$2a$10$WQMloBfgYWA6eFQ/98WBB.Cj9T0NMDwi1FG9U1A0IQocpBBehlYqa",
                    List.of(new SimpleGrantedAuthority("ROLE_USER")));
        }
    }
}
